﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClimateChangeChampionsData
{
    public partial class CarbonCalculation
    {
        #region static methods
        public static CarbonCalculation GetByUserIdAndAudit(DataContext context, int userId, int audit)
        {
            var tmp = from c in context.CarbonCalculations where c.UserId == userId && c.Audit == audit select c;
            return tmp.SingleOrDefault();
        }

        public static bool HasCarbonCalculation(DataContext context, int userId, int audit)
        {
            var tmp = from c in context.CarbonCalculations where c.UserId == userId && c.Audit == audit select c;
            return tmp.Count() != 0;
        }

        public static IList<CarbonCalculation> GetAllByUserId(DataContext context, int userId)
        {
            var tmp = from c in context.CarbonCalculations where c.UserId == userId select c;
            return tmp.OrderBy(x => x.Audit).ToList();
        }
        #endregion

        public DateTime CreatedDateTime
        {
            get { return DateTime.SpecifyKind(_CreatedDateTime, DateTimeKind.Utc).ToLocalTime();}
        }

        public DateTime LastUpdated
        {
            get { return DateTime.SpecifyKind(_LastUpdated, DateTimeKind.Utc).ToLocalTime(); }
        }

        public float KGCO2PerWeek
        {
            get
            {
                if (KWH.HasValue)
                {
                    return (float)KWH.Value * 0.53702f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public int PartyBallonsPerWeek
        {
            get
            {
                return (int)Math.Round(KGCO2PerWeek * 1306);
            }
        }

        public float TonnesCO2PerSchoolYear
        {
            get
            {
                return KGCO2PerWeek * 35 / 1000;
            }
        }

        public int PlanesPerSchoolYear
        {
            get
            {
                return (int)Math.Round(TonnesCO2PerSchoolYear * 1.225);
            }
        }

        public float RecylePaperCO2
        {
            get
            {
                if (RecylePaper != null)
                {
                    return RecylePaper.Value * 1.843f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float RecyleCardCO2
        {
            get
            {
                if (RecyleCard != null)
                {
                    return RecyleCard.Value * 1.843f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float RecyleFoodCO2
        {
            get
            {
                if (RecyleFood != null)
                {
                    return RecyleFood.Value * 2.463f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float RecyleBottlesCO2
        {
            get
            {
                if (RecyleBottles != null)
                {
                    return RecyleBottles.Value * 11.766f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float RecylePackagingCO2
        {
            get
            {
                if (RecylePackaging != null)
                {
                    return RecylePackaging.Value * 9.44f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float RecyleCansCO2
        {
            get
            {
                if (RecyleCans != null)
                {
                    return RecyleCans.Value * 5.074f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float RecyleGlassCO2
        {
            get
            {
                if (RecyleGlass != null)
                {
                    return RecyleGlass.Value * 0.832f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float RecyleOtherCO2
        {
            get
            {
                if (RecyleOther != null)
                {
                    return RecyleOther.Value * 2.381f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float LandPaperCO2
        {
            get
            {
                if (LandPaper != null)
                {
                    return LandPaper.Value * 3.243f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float LandCardCO2
        {
            get
            {
                if (LandCard != null)
                {
                    return LandCard.Value * 3.243f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float LandFoodCO2
        {
            get
            {
                if (LandFood != null)
                {
                    return LandFood.Value * 2.686f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float LandBottlesCO2
        {
            get
            {
                if (LandBottles != null)
                {
                    return LandBottles.Value * 12.788f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float LandPackagingCO2
        {
            get
            {
                if (LandPackaging != null)
                {
                    return LandPackaging.Value * 10.232f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float LandCansCO2
        {
            get
            {
                if (LandCans != null)
                {
                    return LandCans.Value * 16.11f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float LandGlassCO2
        {
            get
            {
                if (LandGlass != null)
                {
                    return LandGlass.Value * 1.416f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float LandOtherCO2
        {
            get
            {
                if (LandOther != null)
                {
                    return LandOther.Value * 2.666f;
                }
                else
                {
                    return 0;
                }
            }
        }

        public float TotalLandCO2PerDay
        {
            get { return LandBottlesCO2 + LandCansCO2 + LandCardCO2 + LandFoodCO2 + LandGlassCO2 + LandOtherCO2 + LandPackagingCO2 + LandPaperCO2; }
        }

        public int TotalLandCO2BalloonsPerDay
        {
            get { return (int)Math.Round(1306f * TotalLandCO2PerDay); }
        }

        public float TotalLandCO2PerSchoolYear
        {
            get { return (TotalLandCO2PerDay * 5 * 38 / 1000); }
        }

        public float TotalLandCO2PlanesPerSchoolYear
        {
            get { return TotalLandCO2PerSchoolYear * 1.225f; }
        }

        public float TotalRecyleCO2PerDay
        {
            get { return RecyleBottlesCO2 + RecyleCansCO2 + RecyleCardCO2 + RecyleFoodCO2 + RecyleGlassCO2 + RecyleOtherCO2 + RecylePackagingCO2 + RecylePaperCO2; }
        }

        public int TotalRecyleCO2BalloonsPerDay
        {
            get { return (int)Math.Round(1306f * TotalRecyleCO2PerDay); }
        }

        public float TotalRecyleCO2PerSchoolYear
        {
            get { return TotalRecyleCO2PerDay *5 * 38 / 1000; }
        }

        public float TotalRecyleCO2PlanesPerSchoolYear
        {
            get { return TotalRecyleCO2PerSchoolYear * 1.225f; }
        }

        public float TotalWasteCO2PerDay
        {
            get { return TotalLandCO2PerDay + TotalRecyleCO2PerDay; }
        }

        public float TotalWasteCO2BalloonsPerWeek
        {
            get { return TotalLandCO2BalloonsPerDay + TotalRecyleCO2BalloonsPerDay; }
        }


        public float TotalWasteCO2PerYear
        {
            get { return TotalLandCO2PerSchoolYear + TotalRecyleCO2PerSchoolYear; }
        }

        public float TotalWastePlanesCO2PerYear
        {
            get { return TotalLandCO2PlanesPerSchoolYear + TotalRecyleCO2PlanesPerSchoolYear; }
        }



        #region validate
        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            if (action == System.Data.Linq.ChangeAction.Insert)
            {
                _CreatedDateTime = DateTime.UtcNow;
                _LastUpdated = DateTime.UtcNow;

            }
            else if (action == System.Data.Linq.ChangeAction.Update)
            {
                _LastUpdated = DateTime.UtcNow;
            }

            Acms.Exceptions.ValidationException exp = new Acms.Exceptions.ValidationException();
            if (this.Audit == 0)
            {
                exp.Add("Audit number must be specified");
            }
            if (this.UserId == 0)
            {
                exp.Add("User Id must be specified");
            }
            if (exp.Items.Length > 0)
            {
                throw exp;
            }
        }
        #endregion
    }
}
